package com.mljr.example.ui.home;


import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.ctakit.sdk.app.widget.tab.home.HomeTabLayout;
import com.ctakit.sdk.app.widget.tab.home.HomeTabManager;
import com.ctakit.sdk.view.annotation.LayoutContentId;
import com.ctakit.sdk.view.annotation.ViewInject;
import com.mljr.example.R;
import com.mljr.example.adapter.HomeTabAdapter;
import com.mljr.example.base.MyBaseActivity;
import com.mljr.example.bean.TabModel;
import com.mljr.example.ui.scan.CaptureActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lizhenmin on 6/30/16.
 */
@LayoutContentId(R.layout.activity_home)
public class HomeActivity extends MyBaseActivity implements HomeTabLayout.OnTabSelectedListener {

    @ViewInject(R.id.mHomeTabLayout)
    private HomeTabLayout mHomeTabLayout;

    private HomeTabAdapter mAdapter;
    private HomeTabManager mTabManager;


    public final static int OPEN_QR_REQUEST_CODE = 1001;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTabManager = new HomeTabManager(getSupportFragmentManager(), this);
        mAdapter = new HomeTabAdapter(this);
        mAdapter.setData(getTabData());
        mAdapter.setOnTabSelectedListener(this);
        mHomeTabLayout.setAdapter(mAdapter);

        mTabManager.setup(R.id.mLayoutRoot, mHomeTabLayout);
    }

    private List<TabModel> getTabData() {
        List<TabModel> data = new ArrayList<>();
        TabModel model = new TabModel();
        model.setTitle("首页");
        model.setIconId(R.drawable.icon_home_tab1);
        model.setFragmentClass(Tab1Fragment.class);
        data.add(model);
        TabModel model1 = new TabModel();
        model1.setTitle("闲逛");
        model1.setIconId(R.drawable.icon_home_tab2);
        model1.setFragmentClass(Tab2Fragment.class);
        data.add(model1);
        TabModel model2 = new TabModel();
        model2.setTitle("扫一扫");
        model2.setIconId(R.mipmap.icon_home_qr);
        model2.setId(mAdapter.ITEM_ID_CENTER);
        data.add(model2);
        TabModel model3 = new TabModel();
        model3.setTitle("酷·分期");
        model3.setIconId(R.drawable.icon_home_tab3);
        model3.setFragmentClass(Tab3Fragment.class);
        data.add(model3);
        TabModel model4 = new TabModel();
        model4.setTitle("我的");
        model4.setIconId(R.drawable.icon_home_tab4);
        model4.setFragmentClass(Tab4Fragment.class);
        data.add(model4);
        return data;
    }


    @Override
    public void onSelected(int position, Bundle bundle) {
        TabModel tabModel = mAdapter.getItem(position);
        if (tabModel != null && tabModel.getId() != 0) {
            switch (tabModel.getId()) {
                case HomeTabAdapter.ITEM_ID_CENTER:
                    scan();//扫一扫
                    break;
            }
        }

    }

    @Override
    public boolean goBack(View v, boolean isPhoneBack) {
        return  mTabManager.getCurrentFragment().goBack(v,isPhoneBack);
    }

    @Override
    public String getPageName() {
        return "HomeActivity";
    }

    @Override
    public void onSelectedAgain(int position, Bundle bundle) {
        TabModel tabModel = mAdapter.getItem(position);
        if (tabModel != null) {
            mTabManager.getCurrentFragment().onSelectedAgain(bundle);
        }
    }

    //扫一扫（二维码）
    public void scan() {
        Intent intent = new Intent(getActivity(), CaptureActivity.class);
        startActivityForResult(intent, OPEN_QR_REQUEST_CODE);
    }

    //返回图片路径
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        switch (requestCode) {
            case OPEN_QR_REQUEST_CODE:
                String urlStr = data.getStringExtra(CaptureActivity.KEY_URL);
                if (!TextUtils.isEmpty(urlStr)&&urlStr.trim().startsWith("http")) {
                    gotoHtml5Activity(urlStr);
                } else {
                    if (urlStr == null) {
                        return;
                    } else {
                        showToastMsg("无效的二维码");
                    }
                }
                break;
        }
    }
}
